package main

import (
	"flag"
	"fmt"
	"github.com/shugen/handlers"
	"github.com/shugen/store"
	svc2 "github.com/shugen/svc"
	"github.com/shugen/utils"
	"log"
	"net/http"
)

var (
	username = flag.String("username", "kevin", "mysql username")
	password = flag.String("password", "123456", "mysql password")
	host     = flag.String("host", "zhangdengdeng.com:53306", "mysql host")
	//host     = flag.String("host", "81.71.37.187:53306", "mysql host")
	//host     = flag.String("host", "localhost:53306", "mysql host")
	dbname   = flag.String("database", "shugen", "database name")
)

type Option struct {
	Value int    `json:"value"`
	Label string `json:"label"`
}

type SelfOption struct {
	Value    string    `json:"value"`
	Label    string    `json:"label"`
	Children []*Option `json:"children"`
}

var (
	SELFR = []SelfOption{
		{"S", "神", SR},
		{"X", "星", XR},
		{"M", "门", MR},
	}

	//神位
	SR = []*Option{
		{1, "值符"},
		{2, "腾蛇"},
		{3, "太阴"},
		{4, "六合"},
		{5, "白虎"},
		{6, "玄武"},
		{7, "九地"},
		{8, "九天"},
		{9, "1+8"},
		{0, "空亡"},
	}

	//星位
	XR = []*Option{

		{1, "天蓬星"},
		{2, "天芮星"},
		{3, "天冲星"},
		{4, "天辅星"},
		{5, "天禽星"},
		{6, "天心星"},
		{7, "天柱星"},
		{8, "天任星"},
		{9, "天英星"},
		{0, "空亡"},
	}

	//门位
	MR = []*Option{

		{1, "休门"},
		{2, "死门"},
		{3, "伤门"},
		{4, "杜门"},
		{5, "平门"},
		{6, "开门"},
		{7, "惊门"},
		{8, "生门"},
		{9, "景门"},
		{0, "空亡"},
	}

	GMR = []Option{
		{0, "宫生门"},
		{1, "门生宫"},
		{2, "宫同门"},
		{3, "宫克门"},
		{4, "门克宫"},
	}

	SMR = []Option{
		{0, "神生门"},
		{1, "门生神"},
		{2, "神同门"},
		{3, "神克门"},
		{4, "门克神"},
	}

	XMR = []Option{
		{0, "星生门"},
		{1, "门生星"},
		{2, "星同门"},
		{3, "星克门"},
		{4, "门克星"},
	}
	TGMR = []Option{
		{0, "门生干"},
		{1, "干生门"},
		{2, "门同干"},
		{3, "门克干"},
		{4, "干克门"},
	}
)

func main() {

	engine, err := store.NewStore(*username, *password, *host, *dbname)

	if err != nil {
		panic(err)
	}

	svc := svc2.NewSvc(engine)

	controller := handlers.NewController(svc)

	//神，星，门位
	http.HandleFunc("/ops/selfr", cors(func(w http.ResponseWriter, r *http.Request) {
		w.Write(utils.Success(SELFR))
	}))
	http.HandleFunc("/ops/self", cors(controller.ISelf))
	http.HandleFunc("/ops/uself", cors(controller.USelf))
	http.HandleFunc("/ops/sself", cors(controller.SSelf))
	http.HandleFunc("/ops/lself", cors(controller.LSelf))

	//门宫的关系
	http.HandleFunc("/ops/gmr", cors(func(w http.ResponseWriter, r *http.Request) {

		r.Header.Get("Authorization")
		w.Write(utils.Success(GMR))
	}))
	http.HandleFunc("/ops/mengong", cors(controller.IMengong))
	http.HandleFunc("/ops/umengong", cors(controller.UMengong))
	http.HandleFunc("/ops/smengong", cors(controller.SMengong))
	http.HandleFunc("/ops/lmengong", cors(controller.LMengong))

	//门神的关系
	http.HandleFunc("/ops/smr", cors(func(w http.ResponseWriter, r *http.Request) {
		w.Write(utils.Success(SMR))
	}))
	http.HandleFunc("/ops/menshen", cors(controller.IMenshen))
	http.HandleFunc("/ops/umenshen", cors(controller.UMenshen))
	http.HandleFunc("/ops/smenshen", cors(controller.SMenshen))
	http.HandleFunc("/ops/lmenshen", cors(controller.LMenshen))

	//门星的关系
	http.HandleFunc("/ops/xmr", cors(func(w http.ResponseWriter, r *http.Request) {
		w.Write(utils.Success(XMR))
	}))
	http.HandleFunc("/ops/menxing", cors(controller.IMenxing))
	http.HandleFunc("/ops/umenxing", cors(controller.UMenxing))
	http.HandleFunc("/ops/smenxing", cors(controller.SMenxing))
	http.HandleFunc("/ops/lmenxing", cors(controller.LMenxing))

	//门天干的关系
	http.HandleFunc("/ops/tgmr", cors(func(w http.ResponseWriter, r *http.Request) {
		w.Write(utils.Success(TGMR))
	}))
	http.HandleFunc("/ops/mentgan", cors(controller.IMentgan))
	http.HandleFunc("/ops/umentgan", cors(controller.UMentgan))
	http.HandleFunc("/ops/smentgan", cors(controller.SMentgan))
	http.HandleFunc("/ops/lmentgan", cors(controller.LMentgan))

	http.HandleFunc("/desc", controller.Desc)

	//if err := http.ListenAndServe(":18878", nil); err != nil {
	//	log.Fatal(err)
	//}
	//https
	if err := http.ListenAndServeTLS(":18878", "./zhangdengdeng.com.crt", "./zhangdengdeng.com.key", nil); err != nil {
		log.Fatal(err)
	}
}

//配置cors
func cors(handler http.HandlerFunc) http.HandlerFunc {

	return func(w http.ResponseWriter, r *http.Request) {

		fmt.Println("...before...")

		w.Header().Set("Access-Control-Allow-Origin", "*")
		//允许请求头修改的类容
		w.Header().Set("Access-Control-Allow-Headers", "Content-Type,Access-Token,x-token")
		//允许使用cookie
		w.Header().Set("Access-Control-Allow-Credentials", "true")
		handler.ServeHTTP(w, r)
		fmt.Println("...after...")
	}

}
